<?php

defined( 'IN_DICE' ) or die( 'Access Error!' );

class index extends control {
  
  public function __construct() {
    parent::__construct();
  }
  
  public function index() {
    $aT = array();
    isset($_GET['m2']) && $_GET['m'] = $_GET['m2'];
    isset($_GET['a2']) && $_GET['a'] = $_GET['a2'];
    if( isset( $_GET['m'] ) && $_GET['m'] == 'index' ) {
      $_GET['m'] = 'game';
    }
    if(!isset($_GET['p']) && isset($_REQUEST['p'])) {
      $_GET['p'] = $_REQUEST['p'];
    }
    foreach ( $_GET as $k => $v ) {
      $aT[$k] = $v;
    } 
    all::$aOut['vars'] = $aT ? http_build_query( $aT ) : '';

    model::platform()->isAdded(); //应用授权，判断用户是否添加此应用或已登录
    $puid = model::platform()->getUserId(); //获得平台ID
    func::display( all::$aOut, 'index.htm' );
  }

  public function stream() {
    $type = S('type');
    $stream = L('stream.'.$type);
    $apiurl = model::platform()->apiurl;
    $baseurl = $apiurl . $stream['attachment']['href'];
    $url = L( $baseurl, array( $this->uid ), false );
    switch ( $type ) {
      case 'mission' :
        $mtype = I('mtype'); //任務類型
        $mbonus = I('mbonus'); //任務獎勵
        $stream['attachment'] = L( $stream['attachment'], array( L('mname.'.$mtype), $mbonus ), false );
        break;
      case 'levelup' :
        $newlevel = I('newlevel'); //升級的新等級
        $lbonus = I('lbonus'); //升級的獎勵
        $stream['attachment'] = L( $stream['attachment'], array( $newlevel, $lbonus ), false );
        break;
      case 'wineup' :
        $winelevel = I('winelevel'); //新的酒量等級
        $stream['attachment'] = L( $stream['attachment'], array( $winelevel ), false );
        break;
      case 'login' :
        $lbonus = I('lbonus'); //登陆奖励
        $stream['attachment'] = L( $stream['attachment'], array( $lbonus ), false );
        break;
      case 'medal' :
        $mdname = S('mdname'); //勳章名稱奖励
        $stream['attachment'] = L( $stream['attachment'], array( $mdname ), false );
        break;
      case 'lottery' :
        $lname = S('lname'); //获奖物品
        $stream['attachment'] = L( $stream['attachment'], array( $lname ), false );
        break;
      case 'win' :
        $stream['attachment'] = L( $stream['attachment'], array(), false );
        break;
      case 'fbonus' :
        $fbonus = I('fbonus');
        $stream['attachment'] = L( $stream['attachment'], array( $fbonus ), false );
        break;
      case 'drunk' :
        $dtoken = S('dtoken');
        $url = L( $baseurl, array( $this->uid, $dtoken ), false );
        $stream['attachment'] = L( $stream['attachment'], array( $this->uid, $dtoken, date('Y-m-d') ), false );
        break;
      case 'chest' :
        $ctoken = S('ctoken');
        $url = L( $baseurl, array( $this->uid, $ctoken ), false );
        $stream['attachment'] = L( $stream['attachment'], array( $this->uid, $ctoken ), false );
        break;
      case 'invite' :
        $stream['attachment'] = L( $stream['attachment'], array( $this->uid ), false );
        break;
      case 'ko' :
        $winner = S('winner');
        $loser = S('loser');
        $stream['attachment'] = L( $stream['attachment'], array( $winner, $loser ), false );
        break;
      case 'rd' :
        $title = S('rdtitle');
        $con = S('rdcon');
        $stream['attachment'] = L( $stream['attachment'], array($title, $con), false );
        break;
      case 'hb' :
        $receiver = S('receiver');
        $hbbonus = I('hbbonus');
        $stream['attachment'] = L( $stream['attachment'], array($receiver, $hbbonus), false );
        break;
      default:
        break;
    }

    $stream['attachment']['href'] = $url; //加上绝对地址的链接
    $stream['attachment']['media'][0]['href'] = $url; //加上绝对地址的链接
    $stream['attachment']['media'][0]['src'] = all::$aOut['imgurl'] . $stream['attachment']['media'][0]['src']; //加上绝对地址的feed图片
    $stream['action_links'][0]['href'] = $url; //加上绝对地址的链接
    
    $aProp = L('streamprop'); //获得并添加stream的描述
    foreach ($aProp as $k => $v) {
      $aProp[$k]['href'] = $url;
    }
    $stream['properties'] = $aProp;

    echo json_encode( $stream );
    exit;
  }

  public function sdofeed() { //盛大发feed出去
    $type = S('type');
    $url = model::platform()->apiurl;
    $aJson = array();
    $aJson['linkurl'] = $url;
    $tmpid = '';
    switch ( $type ) {
      case 'mission' :
        $tmpid = '2000004961291107357';
        $mtype = I('mtype'); //任務類型
        $aJson['mission'] = L('mname.'.$mtype);
        $aJson['chips'] = I('mbonus'); //任務獎勵
        break;
      case 'levelup' :
        $tmpid = '2000004961291107369';
        $aJson['chips'] = I('lbonus'); //升級的獎勵
        $aJson['level'] = I('newlevel'); //升級的新等級
        break;
      case 'wineup' :
        $tmpid = '2000004961291107379';
        $aJson['level'] = I('winelevel'); //新的酒量等級
        break;
      case 'login' :
        $tmpid = '2000004961291107388';
        $aJson['chips'] = I('lbonus'); //登陆奖励
        break;
      default:
        break;
    }
    if( $tmpid ) {
      $aFields['template_id'] = $tmpid;
      $aFields['body_data'] = $aJson;
      $flag = model::platform()->sendFeed( $aFields );
      $flag && die('1');
    }
    die('0');
  }

  public function ivtsingle() {
    all::$aOut['from'] = S('from');
    all::$aU = model::member()->getp(all::$aOut['from'], model::platform()->pfid, true);
    all::$aOut['to'] = S('to');
    func::display( all::$aOut, 'single.htm' );
  }

  public function fandate() { //AJAX获得用户是否已经领取粉丝奖励
    $aOut = array( 'uid' => $this->uid, 'fbonus' => 0 );
    $aG = model::member()->get($this->uid, true);
    !isset( $aG['fbdate'] ) && $aG['fbdate'] = 1;
    if( $aG && ( $aG['fbdate'] != date('Ymd') ) ) $aOut['fbonus'] = 1;
    echo json_encode( $aOut );
    exit;
  }
}